﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.OA.CallbackMethodDictionary;
using Dapper;

namespace BCData.OA.CallbackMethodDictionary
{
    public class CallbackMethodDictionaryData: ICallbackMethodDictionaryData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT CallbackMethodDictionaryId,TaskName,CallbackMethodTitle,CallbackServiceName,CallbackMethodName,CallbackMethodParameters,CreateTime,UpdateTime
                                        FROM OA_CallbackMethodDictionary";
        public CallbackMethodDictionaryData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public async Task<CallbackMethodDictionaryEntity> Add(CallbackMethodDictionaryEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("OA_CallbackMethodDictionary", new string[] { "TaskName", "CallbackMethodTitle", "CallbackServiceName", "CallbackMethodName", "CallbackMethodParameters" });
            sql = string.Format("{0} {1} WHERE CallbackMethodDictionaryId=LAST_INSERT_ID()", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TaskName", entity.TaskName, DbType.AnsiString, size: 150);
            parameters.Add("?CallbackMethodTitle", entity.CallbackMethodTitle, DbType.Int64, size: 100);
            parameters.Add("?CallbackServiceName", entity.CallbackServiceName, DbType.Int64, size: 200);
            parameters.Add("?CallbackMethodName", entity.CallbackMethodName, DbType.Int64, size: 100);
            parameters.Add("?CallbackMethodParameters", entity.CallbackMethodParameters, DbType.Int64, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<CallbackMethodDictionaryEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<CallbackMethodDictionaryEntity> Get(string taskName)
        {
            string sql = string.Format("{0} WHERE TaskName=?TaskName", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TaskName", taskName, DbType.AnsiString, size: 150);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<CallbackMethodDictionaryEntity>(sql, parameters);
        }
    }
}
